package p300;

import java.util.Stack;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @description Leetcode_331 <br>
 * @date 2021/3/12 22:16 <br>
 */
public class Leetcode_331 {
    public boolean isValidSerialization(String preorder) {
        Stack<String> stack = new Stack<>();
        for (String c : preorder.split(",")) {
            if (!stack.empty() && "#".equals(stack.peek()) && "#".equals(c)) {
                if (stack.size() == 1) {
                    return false;
                }
                while (stack.size() >= 2 && "#".equals(stack.peek())) {
                    stack.pop();
                    stack.pop();
                }
            }
            stack.push(c);
            System.out.println(stack);
        }
        return stack.size() == 1 && "#".equals(stack.peek());
    }

    public static void main(String[] args) {
        Leetcode_331 l = new Leetcode_331();
        System.out.println(l.isValidSerialization("9,#,92,#,#"));
    }
}
